package com.sky.loveshop.utils;

import com.sky.loveshop.config.LoggingDecoder;
import feign.Feign;
import feign.Request;
import feign.RequestInterceptor;
import feign.jackson.JacksonDecoder;
import feign.jackson.JacksonEncoder;

import java.util.concurrent.TimeUnit;

/**
 * @author xcy
 * @version V1.0
 * @description
 * @date 2024/9/11 22:10
 */
public class FeignUtil {

    public static <T> T getDefaultFeignClient(Class<T> clazz, String domain) {
        return Feign.builder()
                .options(new Request.Options(10L, TimeUnit.SECONDS, 10L, TimeUnit.SECONDS, true))
                .decoder(new JacksonDecoder())
                .encoder(new JacksonEncoder())
                .target(clazz, domain);
    }

    public static <T> T getDefaultFeignClient(Class<T> clazz, String domain, RequestInterceptor requestInterceptor) {
        return Feign.builder()
                .options(new Request.Options(10L, TimeUnit.SECONDS, 10L, TimeUnit.SECONDS, true))
                //.decoder(new JacksonDecoder())
                .decoder(new LoggingDecoder(new JacksonDecoder())) // 使用自定义解码器将JacksonDecoder再包装一层
                .encoder(new JacksonEncoder())
                .requestInterceptor(requestInterceptor)
                .target(clazz, domain);
    }
}